<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="id" prop="id">
        <el-input v-model="dataForm.id" placeholder="id"></el-input>
      </el-form-item>
      <el-form-item label="id" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="id"></el-input>
      </el-form-item>
      <el-form-item label="用户名称" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名称"></el-input>
      </el-form-item>
      <el-form-item label="树木等级ID" prop="levelId">
        <el-input v-model="dataForm.levelId" placeholder="树木等级ID"></el-input>
      </el-form-item>
      <el-form-item label="树木等级名称" prop="levelName">
        <el-input v-model="dataForm.levelName" placeholder="树木等级名称"></el-input>
      </el-form-item>
      <el-form-item label="等级" prop="level">
        <el-input v-model="dataForm.level" placeholder="等级"></el-input>
      </el-form-item>
      <el-form-item label="当前浇水次数" prop="wateringCount">
        <el-input v-model="dataForm.wateringCount" placeholder="当前浇水次数"></el-input>
      </el-form-item>
      <el-form-item label="所需浇水总次数" prop="wateringTotal">
        <el-input v-model="dataForm.wateringTotal" placeholder="所需浇水总次数"></el-input>
      </el-form-item>
      <el-form-item label="上次浇水时间" prop="lastWatered">
        <el-date-picker type="datetime" placeholder="上次浇水时间" v-model="dataForm.lastWatered" format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="奖励状态 0:未领 1:已领" prop="status">
        <el-input v-model="dataForm.status" placeholder="奖励状态 0:未领 1:已领"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  userId: "",
  username: "",
  levelId: "",
  levelName: "",
  level: "",
  wateringCount: "",
  wateringTotal: "",
  lastWatered: "",
  status: "",
  creator: "",
  createDate: "",
  updater: "",
  updateDate: "",
});

const rules = ref({
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/base/trees/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/base/trees", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>